"""
User Management Models
"""
from django.db import models
from django.contrib.auth.models import User


class UserGroup(models.Model):
    """用户组模型"""
    
    CATEGORY_CHOICES = [
        ('test', '测试部'),
        ('development', '开发部'),
        ('production', '生产部'),
        ('leadership', '部门领导'),
        ('other', '其他'),
    ]
    
    name = models.CharField(max_length=100, unique=True, verbose_name='组名')
    category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, default='other', verbose_name='分类')
    description = models.TextField(blank=True, verbose_name='描述')
    users = models.ManyToManyField(User, related_name='user_groups', blank=True, verbose_name='组成员')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '用户组'
        verbose_name_plural = '用户组'
        ordering = ['category', 'name']
    
    def __str__(self):
        return f"{self.get_category_display()} - {self.name}"
    
    @property
    def user_count(self):
        """获取组内用户数量"""
        return self.users.count()
